package com.smarttest.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.smarttest.common.beans.SFTPOPBean;

/**
 * ftp上传下载工具类
 * <p>Title: FtpUtil</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.com</p> 
 * @author	入云龙
 * @date	2015年7月29日下午8:11:51
 * @version 1.0
 */
public class FtpUtil {

	/** 
	 * Description: 向FTP服务器上传文件 
	 * @param host FTP服务器hostname 
	 * @param port FTP服务器端口 
	 * @param username FTP登录账号 
	 * @param password FTP登录密码 
	 * @param basePath FTP服务器基础目录
	 * @param filePath FTP服务器文件存放路径。例如分日期存放：/2015/01/01。文件的路径为basePath+filePath
	 * @param filename 上传到FTP服务器上的文件名 
	 * @param input 输入流 
	 * @return 成功返回true，否则返回false 
	 */  
	public static boolean uploadFile(String host, int port, String username, String password,
			String filePath, String filename, InputStream input) {
	        ChannelSftp sftp = null;  
	        Channel channel = null;  
	        Session sshSession = null;  
	        try {  
	            JSch jsch = new JSch();  
	            jsch.getSession(username, host, port);  
	            sshSession = jsch.getSession(username, host, port);  
	            sshSession.setPassword(password);  
	            Properties sshConfig = new Properties();  
	            sshConfig.put("StrictHostKeyChecking", "no");  
	            sshSession.setConfig(sshConfig);  
	            sshSession.connect();  
	            channel = sshSession.openChannel("sftp");  
	            channel.connect();  
	            sftp = (ChannelSftp) channel;  
	            System.out.println("==========="+filePath+filename);
	            sftp.put(input, filePath+filename);
	            return true;
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            closeChannel(sftp);  
	            closeChannel(channel);  
	            closeSession(sshSession);  
	        }  
	        return false;
	}
	
	/** 
	 * Description: 从FTP服务器下载文件 
	 * @param host FTP服务器hostname 
	 * @param port FTP服务器端口 
	 * @param username FTP登录账号 
	 * @param password FTP登录密码 
	 * @param remotePath FTP服务器上的相对路径 
	 * @param fileName 要下载的文件名 
	 * @param localPath 下载后保存到本地的路径 
	 * @return 
	 */  
	public static SFTPOPBean downloadSingleFile(String host, int port, String username, String password, String url) {
		ChannelSftp sftp = null;  
        Channel channel = null;  
        Session sshSession = null;  
        try {  
            JSch jsch = new JSch();  
            jsch.getSession(username, host, port);  
            sshSession = jsch.getSession(username, host, port);  
            sshSession.setPassword(password);  
            Properties sshConfig = new Properties();  
            sshConfig.put("StrictHostKeyChecking", "no");  
            sshSession.setConfig(sshConfig);  
            sshSession.connect();  
            channel = sshSession.openChannel("sftp");  
            channel.connect();  
            sftp = (ChannelSftp) channel;  
            InputStream in = sftp.get(url);
            SFTPOPBean context = new SFTPOPBean(sftp,channel,sshSession,in);
            return context;
        } catch (Exception e) {  
            e.printStackTrace();
            closeChannel(sftp);  
            closeChannel(channel);  
            closeSession(sshSession);  
        }  
        return null;
	}
	
	
	
	 private static List<String> listFileNames(String host, int port, String username, final String password, String dir) {  
	        List<String> list = new ArrayList<String>();  
	        ChannelSftp sftp = null;  
	        Channel channel = null;  
	        Session sshSession = null;  
	        try {  
	            JSch jsch = new JSch();  
	            jsch.getSession(username, host, port);  
	            sshSession = jsch.getSession(username, host, port);  
	            sshSession.setPassword(password);  
	            Properties sshConfig = new Properties();  
	            sshConfig.put("StrictHostKeyChecking", "no");  
	            sshSession.setConfig(sshConfig);  
	            sshSession.connect();  
	            channel = sshSession.openChannel("sftp");  
	            channel.connect();  
	            sftp = (ChannelSftp) channel;  
	            Vector<?> vector = sftp.ls(dir);  
	            for (Object item:vector) {  
	                LsEntry entry = (LsEntry) item;  
	                System.out.println(entry.getFilename());  
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            closeChannel(sftp);  
	            closeChannel(channel);  
	            closeSession(sshSession);  
	        }  
	        return list;  
	    }  
	  
	    private static void closeChannel(Channel channel) {  
	        if (channel != null) {  
	            if (channel.isConnected()) {  
	                channel.disconnect();  
	            }  
	        }  
	    }  
	  
	    private static void closeSession(Session session) {  
	        if (session != null) {  
	            if (session.isConnected()) {  
	                session.disconnect();  
	            }  
	        }  
	    }  
	
	
	public static void main(String[] args) throws IOException {
//		try {  
//	        FileInputStream in=new FileInputStream(new File("D:\\controller.log"));  
//	        boolean flag = uploadFile("172.16.1.19", 22, "ftpuser", "ftpuser", "/home/ftpuser/", "controller.log", in);  
//	        System.out.println(flag);  
//	    } catch (FileNotFoundException e) {  
//	        e.printStackTrace();  
//	    }  
		
//		InputStream in = downloadSingleFile("172.16.1.19", 22, "ftpuser", "ftpuser","/home/ftpuser/autocases/test07.jmx");
//		if(in!=null){
//			byte[] buffer = new byte[1024]; 
//			FileOutputStream out = new FileOutputStream(new File("D:\\xzy.jmx"));
//			while(in.read(buffer)!=-1){
//				out.write(buffer);
//			}
//		}
	}
}
